/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.isseiaoki.simplecropview.slice;

import com.isseiaoki.simplecropview.CropImageView;
import com.isseiaoki.simplecropview.ResourceTable;
import com.isseiaoki.simplecropview.callback.CropCallback;
import com.isseiaoki.simplecropview.callback.LoadCallback;
import com.isseiaoki.simplecropview.callback.SaveCallback;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.global.resource.ResourceManager;
import ohos.media.image.ImagePacker;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.utils.net.Uri;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

/**
 * basic示例
 */
public class CropImageAbilitySlice extends AbilitySlice {
    private CropImageView cropImageView;
    private Uri uri = null;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_crop_image);
        cropImageView = (CropImageView) findComponentById(ResourceTable.Id_cropImageView);
        Image image1 = (Image) findComponentById(ResourceTable.Id_img_test);
        ResourceManager resourceManager = getResourceManager();
        File file = null;
        try {
            file = new File(getContext().getFilesDir() + "/test11.png");
            if (!file.exists()) {
                file.createNewFile();
            }
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(resourceManager.getResource(ResourceTable.Media_sample1), sourceOptions);
            ImagePacker imagePacker = ImagePacker.create();
            ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions();
            packingOptions.format = "image/jpeg";
            packingOptions.quality = 90;
            imagePacker.initializePacking(bos, packingOptions);
            imagePacker.addImage(imageSource);
            imagePacker.finalizePacking();
            bos.flush();
            bos.close();
            uri = Uri.getUriFromFile(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        cropImageView.load(uri).execute(new LoadCallback() {
            @Override
            public void onSuccess(PixelMap PixelMap) {
            }

            @Override
            public void onError(Throwable e) {
            }
        });
        Button btnCrop = (Button) findComponentById(ResourceTable.Id_btn_crop);
        btnCrop.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                cropImageView.crop(uri).execute(new CropCallback() {
                    @Override
                    public void onSuccess(PixelMap cropped) {
                        final String uriPath = "file://"+getContext().getFilesDir()+"/test33.jpg";
                        cropImageView.save(cropped)
                                .execute(Uri.parse(uriPath), new SaveCallback() {
                                    @Override
                                    public void onSuccess(Uri uri) {
                                    }

                                    @Override
                                    public void onError(Throwable e) {

                                    }
                                });
                        getAbility().getContext().getUITaskDispatcher().asyncDispatch(new Runnable() {
                            @Override
                            public void run() {
                                image1.setVisibility(Component.VISIBLE);
                                image1.setPixelMap(cropped);
                                cropImageView.setVisibility(Component.INVISIBLE);
                            }
                        });

                    }

                    @Override
                    public void onError(Throwable e) {
                    }
                });
            }
        });

        Button btnFixImag = (Button) findComponentById(ResourceTable.Id_btn_fiximage);
        Button btnSqure = (Button) findComponentById(ResourceTable.Id_btn_squre);
        Button btn34 = (Button) findComponentById(ResourceTable.Id_btn_3_4);
        Button btn43 = (Button) findComponentById(ResourceTable.Id_btn_4_3);
        Button btnFree = (Button) findComponentById(ResourceTable.Id_btn_free);
        Button btnCircle = (Button) findComponentById(ResourceTable.Id_btn_circle);

        btnFixImag.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                image1.setVisibility(Component.HIDE);
                cropImageView.setVisibility(Component.VISIBLE);
                cropImageView.setCropMode(CropImageView.CropMode.FIT_IMAGE);
            }
        });

        btnSqure.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                image1.setVisibility(Component.HIDE);
                cropImageView.setVisibility(Component.VISIBLE);
                cropImageView.setCropMode(CropImageView.CropMode.SQUARE);
            }
        });

        btn34.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                cropImageView.setCropMode(CropImageView.CropMode.RATIO_3_4);
            }
        });

        btn43.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                image1.setVisibility(Component.HIDE);
                cropImageView.setVisibility(Component.VISIBLE);
                cropImageView.setCropMode(CropImageView.CropMode.RATIO_4_3);
            }
        });

        btnFree.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                image1.setVisibility(Component.HIDE);
                cropImageView.setVisibility(Component.VISIBLE);
                cropImageView.setCropMode(CropImageView.CropMode.FREE);
            }
        });

        btnCircle.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                image1.setVisibility(Component.HIDE);
                cropImageView.setVisibility(Component.VISIBLE);
                cropImageView.setCropMode(CropImageView.CropMode.CIRCLE);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
